package com.tinyseed.emcp.crawler.utils;

import com.tinyseed.emcp.crawler.base.OkHttpClientFactory;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @version Revision 1.0.0
 * @版权：
 * @author: Howard
 * @email:
 * @see:
 * @创建日期：2020/3/12
 * @功能说明：登录
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2020/3/12 wuxiran        创建
 * @end
 */
@Component
@Slf4j
public class LoginUtils {

    @Value("${crawler.username}")
    private String username = "administrator";

    @Value("${crawler.password}")
    private String password = "key123456";

    private OkHttpClient loginClient;

    public LoginUtils() {
        this.loginClient = mockLogin(username, password);
    }
    @NotNull
    public static OkHttpClient getOkHttpClient(String user, String password, Logger log) {
        OkHttpClient client = OkHttpClientFactory.create();
        Request request = new Request.Builder()
                .url("http://119.3.187.215")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36")
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Host", "119.3.187.215")
                .post(new FormBody.Builder()
                        .add("ReturnUrl", "")
                        .add("LoginName", user)
                        .add("Password", password)
                        .build()
                )
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                log.info(user + "登录成功 !");
            } else {
                log.info("code = {}, message = {}", response.code(), response.message());

            }
        } catch (IOException e) {
            log.info("登录失败 !");
        }

        return client;
    }
    public static OkHttpClient getLoginClient() {
        OkHttpClient client = OkHttpClientFactory.create();
        Request request = new Request.Builder()
                .url("http://119.3.187.215")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36")
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Host", "119.3.187.215")
                .post(new FormBody.Builder()
                        .add("ReturnUrl", "")
                        .add("LoginName", "administrator")
                        .add("Password", "key123456")
                        .build()
                )
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                log.info("administrator" + "登录成功 !");
            } else {
                log.info("code = {}, message = {}", response.code(), response.message());

            }
        } catch (IOException e) {
            log.info("登录失败 !");
        }

        return client;
    }


    public OkHttpClient mockLogin(String user, String password) {
        return getOkHttpClient(user, password, log);
    }

}
